package resources;

import game_engine.display.GEUtil;

import java.awt.Color;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;

import javax.imageio.ImageIO;

public class ImageHandler {

	private static ImageHandler instance;
	private HashMap<String, BufferedImage> images;
	private HashMap<Integer, BufferedImage> imageNumbers;
	private Color transColor = new Color(0xbfdcbf);
	private Integer[][] link;

	private ImageHandler() {
		images = new HashMap<String, BufferedImage>();
		imageNumbers = new HashMap<Integer, BufferedImage>();
	}

	public BufferedImage loadImage(String image, Color transColor) {
		if(transColor==null) transColor = this.transColor;
		if (!images.containsKey(image)) {
			try {
				URL imgURL = getClass().getResource("/" + image);
				if (imgURL != null) {
					images.put(image, GEUtil.makeColorTransparent(Toolkit.getDefaultToolkit().getImage(imgURL), transColor));
				} else
					images.put(image, GEUtil.makeColorTransparent(ImageIO.read(new File("resources/" + image)), transColor));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return images.get(image);
	}

	private String getImageURL(String image) {
		URL imgURL = getClass().getResource("/image" + image);
		if (imgURL != null) {
			return imgURL.getPath();
		} else
			return "resources/image" + image;
	}

	public BufferedImage loadBackgroundImage(String image, int count) {
		if (!images.containsKey(image)) {
			try {
				URL imgURL = getClass().getResource("/" + image);
				if (imgURL != null)
					images.put(image, GEUtil.makeColorLighter(Toolkit.getDefaultToolkit().getImage(imgURL)));
				else 
					images.put(image, GEUtil.makeColorLighter(ImageIO.read(new File("resources/" + image))));
				imageNumbers.put(count, images.get(image));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return images.get(image);
	}

	public void loadWorld(String world) {
		try {
			BufferedReader br = new BufferedReader(new FileReader(world));
			String line;
			int count = 0;
			while (!(line = br.readLine()).equals("")) {
				loadBackgroundImage("image/world/"+line, count);
				count++;
			}
			link = new Integer[count][5];
			int j = 0;
			while ((line = br.readLine()) != null) {
				String[] splittedLine = line.split(";");
				for (int i = 1; i < 6; i++) {
					if (!splittedLine[i].equals("_")) {
						link[j][i-1] = Integer.parseInt(splittedLine[i]);
					}
					else
						link[j][i-1] = null;
				}
				j++;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public Integer[][] getLink() {
		return link;
	}

	public HashMap<Integer, BufferedImage> getImageNumbers() {
		return imageNumbers;
	}

	public static ImageHandler getInstance() {
		if (instance == null)
			instance = new ImageHandler();
		return instance;
	}
}
